package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询指定用户的全部地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(BaseContext.getCurrentId()!=null,AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 设置默认地址。
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){ // 其实只有id: "1538456667639836673" 是地址的id
        log.info("addressBook:{}",addressBook);
        // 注意是 LambdaUpdateWrapper
        // is_default 设置为1
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        // 让所有当前用户设为默认0， 然后再进行更改。
        queryWrapper.set(AddressBook::getIsDefault,0);
        // sql: update address_book set is_default = 0 where user_id = ?
        addressBookService.update(queryWrapper);

        // 更改默认1
        addressBook.setIsDefault(1);
        // sql:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    // 视频还有两个方法，但是 我没有找到前端发来的 ajax请求

    /**
     * 加入购物车之后 展示默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        if(one!=null){
            return R.success(one);
        }
        return R.error("没有找到默认地址");
    }

    /**
     * 根据id查找地址   回显数据
     * @param addressBookId
     * @return
     */
    @GetMapping("/{addressBookId}")
    public R<AddressBook> get(@PathVariable Long addressBookId){
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if(addressBook!=null) {
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }

    /**
     * 修改地址信息。
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> edit(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功！");
    }

    /**
     * 删除购物车信息。
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功！");
    }



}
